package cate.game.farm.local.adventure;

import cate.game.framework.ModuleMan;
import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class AdventureManager extends ModuleMan {
    @NoteField(value = "剧本记录",detail = "")
    private Map<String, List<PlayPO>> playMap = new ConcurrentHashMap<>();

    @Override
    public void initDependent() throws Exception {
    }

    public void recordPlay(String roleUid, PlayPO play) {
        playMap.computeIfAbsent(roleUid, k -> new ArrayList<>()).add(play);
        log.debug("冒险模块【战斗结算】 缓存了一场录像 当前缓存了{}个玩家的录像", playMap.keySet().size());
    }

    public List<PlayPO> removePlay(String roleUid) {
        List<PlayPO> plays = playMap.getOrDefault(roleUid, new ArrayList<>());
        playMap.remove(roleUid);
        log.debug("冒险模块【战斗失败】 手动释放了一个玩家的录像 当前缓存了{}个玩家的录像", playMap.keySet().size());
        return plays;
    }

    public List<PlayPO> tryUpdate(Role role, PlayPO play, int combatNum) {
        List<PlayPO> plays = playMap.get(role.getUid());
        if (plays == null) {
            plays = new ArrayList<>();
        } else {
            playMap.remove(role.getUid());
            log.debug("冒险模块【正常结束】 释放了一个玩家的录像 当前缓存了{}个玩家的录像", playMap.keySet().size());
        }
        plays.add(play);
        return plays;
    }

    @Override
    public void shutdown(){
    }
}
